package org.tjc.net.manager;

import org.tjc.net.manager.config.json.ContextFacade;
import org.tjc.net.manager.config.json.NgServerModule;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: tjc
 * @description
 * @Date Created in 17:22 2020-11-25
 */
public class HttpConnector {
    HttpProcessor processor;
    ServerSocket server = null;
    NgServerModule serverConf;
    private static int port;

    private ContextFacade context;



    public HttpConnector(ContextFacade context) {
        this.context = context;
        serverConf = context.getServer();
        port = serverConf.getPort();
    }


    private void init() {
        processor = new HttpProcessor(context);
    }

    public void start() {
        init();
        try {
            server = new ServerSocket(port);
            while(true) {
                Socket socket = server.accept();
                processor.process(socket);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    public void close() {
        try {
            if (server != null) {
                server.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
